/*************************************************************
 * 
 * CopyRight(c)2007, http://www.moorwind.com/
 * email:ezhung@gmail.com
 * for free, but a email permission would be nice
 * 
 ************************************************************/
package kono.utils.collections
{
	import kono.utils.ListNode;
	
	//包含虚拟结点（一个头结点，一个尾结点）的list， simple list contains a reference to head and tail node
	
	public class List implements Icollection
	{
		private var _count:uint;
		
		private var _head:ListNode;
		private var _tail:ListNode;
		
		public function List()
		{
			_head = _tail = null;
			_count = 0;
		}
		
		public function get head():ListNode
		{
			return _head;
		}
		
		public function get tail():ListNode
		{
			return _tail;
		}
		
		//追加一个结点数据， append a item to list
		public function append(item:*):ListNode
		{
			var newNode:ListNode = new ListNode(item);
			if(_head)
			{
				_tail.link = newNode;
				_tail = newNode;
			}
			else
			{
				_head = _tail = newNode;
			}
			_count++;
			return newNode;
		}			
		
		//追加一个结点数据， prepend a item to list
		public function prepend(item:*):ListNode
		{
			var newNode:ListNode = new ListNode(item);
			if(_head)
			{
				newNode.link = _head;
				_head = newNode;
			}
			else
			{
				_head = _tail = newNode;
			}
			_count++;
			return newNode;
		}
		
		//删除头结点， remove the node of the head
		public function removeHead():void
		{
			if(!_head) return;
			if(_head == _tail)
			{
				_head = _tail = null;
			}
			else
			{
				var node:ListNode = _head;
				_head = _head.link;
				node.link = null;
				if(_head == null) _tail == null;
			}
			_count--;
		}
		
		//删除尾结点， remove the node of the tail
		public function removeTail():void
		{
			if(!_head) return;
			if(_head == _tail)
			{
				_head = _tail = null;
			}
			else
			{
				var node:ListNode = _head;
				while (node.link != _tail)
				{
					node = node.link;
				}
				_tail = node;
				node.link = null;				
			}
			_count--;
		}
		
		//清空list， unlink all items in the list
		public function clear():void
		{
			var node:ListNode = _head;
			_head = null;
			
			var next:ListNode;
			while(node)
			{
				next = node.link;
				node.link = null;
				node = next;
			}
			_count =0;
		}
		
		//检查列表是否包含给定数据， check if the list contains the given item
		public function contains(item:*):Boolean
		{
			var node:ListNode = _head;
			while(node)
			{
				if(node.nodeData == item)
					return true;
				node = node.link;
			}
			
			return false;
		}
		
		//得到list的大小， get the size of the list
		public function get size():uint
		{
			return _count;
		}
		
		public function getListIterator():ListIterator
		{
			return new ListIterator(_head,this);
		}
		
		public function getIterator():Iiterator
		{
			return new ListIterator(_head,this);
		}
		
		//转换list数据为array， convert the list to array
		public function toArray():Array
		{
			var _array:Array = new Array();
			var _node:ListNode = _head;
			while(_node)
			{
				_array.push(_node.nodeData);
				_node = _node.link;
			}
			return _array;
		}
		
		//描述当前list， rerurn a string represent the list		
		public function toString():String
		{
			return "[List, size=" + _count + "]";
		}
		
	}
}